package com.example.imageselct;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.smallkingson.ylib.core.Yr;
import com.smallkingson.ylib.core.utils.YDensitys;
import com.smallkingson.ylib.view.adapter.YRecyclerAdapter;
import com.smallkingson.ylib.view.utils.YViews;

import java.util.ArrayList;
import java.util.List;

public class RecordMediaSelectActivity extends Activity {

    List<List<MessageWrapper>> lists = new ArrayList<>();

    Bitmap bitmap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initData();
        RecyclerView mRecyclerView = findViewById(R.id.msg_image_select_rv);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
        mRecyclerView.setAdapter(new YRecyclerAdapter() {
            @Override
            protected void setupView(YViewHolder holder, int position) {
                final List<MessageWrapper> messageWrappers = lists.get(position);

                final RecyclerView recyclerView = (RecyclerView) $view(R.id.recycleView);
                recyclerView.setLayoutManager(new GridLayoutManager(getApplicationContext(), 4));
                recyclerView.setAdapter(new YRecyclerAdapter() {
                    @Override
                    protected void setupView(YViewHolder holder, int position) {
                        resize(holder.itemView, position);
                        MessageWrapper messageWrapper = messageWrappers.get(position);
                        if (bitmap == null) {
                            bitmap = BitmapFactory.decodeResource(getResources(), messageWrapper.res);
                        }
                        $img(R.id.imageView).setImageBitmap(bitmap);
                    }

                    private void resize(View itemView, int position) {
                        int spanCount = ((GridLayoutManager) recyclerView.getLayoutManager()).getSpanCount();
                        int dividingLineWidth = (int) (YDensitys.dip2FloatPx(getApplicationContext(), 2) + 0.5f);
                        dividingLineWidth = 4;
                        int itemWidth = YViews.getScreenWidth(getApplicationContext()) / 4; //注：每一行间距3px
                        RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) itemView.getLayoutParams();
                        layoutParams.width = itemWidth;
                        layoutParams.height = itemWidth;
                        int x = (int) (dividingLineWidth / 1.33f + 0.5f);
                        int _33x = (int) (x * 0.33f + 0.5f);
                        int _66x = (int) (x * 0.66f + 0.5f);
                        if (position%4==0) {
                            itemView.setPadding(0, 0, x, x);
                        } else if (position%4==1) {
                            itemView.setPadding(_33x, 0, _66x, x);
                        } else if (position%4==2) {
                            itemView.setPadding(_66x, 0, _33x, x);
                        } else if (position%4==3) {
                            itemView.setPadding(x, 0, 0, x);
                        }
//                        itemView.setLayoutParams(layoutParams);
                    }

                    @Override
                    public int getItemCount() {
                        return messageWrappers.size();
                    }

                    @Override
                    protected int getLayoutRes(int viewType) {
                        return R.layout.msg_image_select_recycler_item_item;
                    }

                });
            }

            @Override
            public int getItemCount() {
                return lists.size();
            }

            @Override
            protected int getLayoutRes(int viewType) {
                return R.layout.msg_image_select_recycler_item;
            }
        });
    }

    private void initData() {

        for (int i = 0; i < 15; i++) {
            List<MessageWrapper> messageWrappers = new ArrayList<>();
            for (int j = 0; j < 10; j++) {
                messageWrappers.add(new MessageWrapper(R.mipmap.test));
            }
            lists.add(messageWrappers);
        }
    }

    public static class MessageWrapper {
        int res;

        public MessageWrapper(int res) {
            this.res = res;
        }
    }
}
